package com.zhenhe.core.common.role;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.zhenhe.core.common.entity.TUser;
import com.zhenhe.core.common.enums.impl.EnumList.RoleType;
import com.zhenhe.core.common.exception.AuthenticationException;
import com.zhenhe.core.common.global.signIn.LoginInfoUtil;
import com.zhenhe.core.common.utils.StringUtils;

@Aspect
@Component
public class RoleAop {

	private static final Logger log = LoggerFactory.getLogger(RoleAop.class);
	
	
	@Before("@annotation(role)")
	public void before(Role role){
		TUser user = LoginInfoUtil.getLoginInfo().getLoginUser();
		if(StringUtils.anyIsEmpty(user,user.getUserRole())){
			log.debug("{}试图访问越权内容", user);
			throw new AuthenticationException("无权限");
		}
		RoleType[] needRole = role.needRole();
		for(RoleType r : needRole){
			if(r.getCode().equals(user.getUserRole())){
				return ;
			}
		}
		log.warn("{}试图访问越权内容", user.getUserAccount());
		throw new AuthenticationException("无权限");
	}
	
}
